package cn.zero.letaomallspringboot.controller;

import cn.zero.letaomallspringboot.Constant.ResultCode;
import cn.zero.letaomallspringboot.common.PageResult;
import cn.zero.letaomallspringboot.common.Result;
import cn.zero.letaomallspringboot.entity.Good;
import cn.zero.letaomallspringboot.service.IGoodService;
import cn.zero.letaomallspringboot.service.impl.GoodService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/good")
public class GoodController {
    private final IGoodService goodService;

    private static final Logger logger = LoggerFactory.getLogger(GoodController.class);

    public GoodController(IGoodService goodService) {
        this.goodService = goodService;
    }

    @GetMapping("/list")
    public Result<PageResult<Good>> goodList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String shopName,
            @RequestParam(required = false) String goodType) {

        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("size", size);
        if (name != null && !name.trim().isEmpty()) {
            params.put("name", name.trim());
        }
        if (shopName != null && !shopName.trim().isEmpty()) {
            params.put("shopName", shopName.trim());
        }
        if (goodType != null && !goodType.trim().isEmpty()) {
            params.put("goodType", goodType.trim());
        }

        PageResult<Good> pageResult = goodService.goodList(params);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result<Good> getGood(@PathVariable Integer id) {
        logger.info("获取商品列表: {}", id);
        Good good = goodService.getGood(id);
        if (good == null) {
            return Result.fail(ResultCode.GOOD_NOT_FOUND); // 商品不存在
        }
        return Result.success(good);
    }

    @PostMapping
    public Result<Good> addGood(@RequestBody Good good) {
        logger.info("添加商品: {}", good);
        boolean success = goodService.addGood(good);
        if (!success) {
            return Result.fail(ResultCode.GOOD_INSERT_FAIL); // 商品新增失败
        }
        return Result.success(good);
    }

    @PutMapping("/{id}")
    public Result<Good> updateGood(@PathVariable Integer id, @RequestBody Good good) {
        logger.info("更新商品: {},{}", id, good);
        good.setGoodId(Long.valueOf(id)); // 设置ID以确保更新正确记录
        boolean success = goodService.updateGood(good);
        if (!success) {
            return Result.fail(ResultCode.GOOD_UPDATE_FAIL); // 商品更新失败
        }
        return Result.success(good);
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteGood(@PathVariable Integer id) {
        logger.info("根据id删除商品: {}",id);
        Good good = goodService.getGood(id);
        if (good == null) {
            return Result.fail(ResultCode.GOOD_NOT_FOUND); // 商品不存在
        }
        boolean success = goodService.deleteGood(id);
        if (!success) {
            return Result.fail(ResultCode.GOOD_DELETE_FAIL); // 商品删除失败
        }
        return Result.success(null);
    }

    @DeleteMapping("/batch")
    public Result<Void> batchDeleteGood(@RequestBody List<Integer> ids) {
        logger.info("批量删除商品");
        boolean success = goodService.batchDeleteGood(ids);
        if (!success) {
            return Result.fail(ResultCode.GOOD_BATCH_DELETE_FAIL); // 商品批量删除失败
        }
        return Result.success(null);
    }
}